﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Web;
using System.IO;

namespace PushMessage
{
    /// <summary>
    /// 日志類
    /// </summary>
    public class Log
    {
        /// <summary>
        /// 行換符
        /// </summary>
        static string newLine = System.Environment.NewLine;

        /// <summary>
        /// 系統標識
        /// </summary>
        static string SystemFlag = "Msg";

        #region 獲取當前時間的yyyy-MM-dd格式字符串
        /// <summary>
        /// 獲取當前時間的yyyy-MM-dd格式字符串
        /// </summary>
        /// <returns></returns>
        public static string Getyyyy_MM_dd()
        {
            return DateTime.Now.ToString("yyyy-MM-dd");
        }
        #endregion

        #region 獲取當前時間的yyyy-MM-dd HH:mm:ss格式字符串
        /// <summary>
        /// 獲取當前時間的yyyy-MM-dd HH:mm:ss格式字符串
        /// </summary>
        /// <returns></returns>
        public static string Getyyyy_MM_dd_HH_mm_ss()
        {
            return DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
        }
        #endregion

        #region 创建文件夹 + static bool CreateDirectory(string filePath)
        /// <summary>
        /// 创建文件夹
        /// </summary>
        /// <param name="filePath">文件路径</param>
        /// <returns>文件夹是否存在</returns>
        public static bool CreateDirectory(string filePath)
        {
            string directoryPath = Path.GetDirectoryName(filePath);
            if (!Directory.Exists(directoryPath))
            {
                Directory.CreateDirectory(directoryPath);
                return false;
            }
            return true;
        } 
        #endregion

        #region 添加錯誤日志 + static void AddErrorLog(string content)
        /// <summary>
        /// 添加錯誤日志
        /// </summary>
        /// <param name="content">日志內容</param>
        public static void AddErrorLog(string content)
        {
            string filePath = AppDomain.CurrentDomain.BaseDirectory + Getyyyy_MM_dd() + "_" + SystemFlag + "_Error.log";
            CreateDirectory(filePath);
            File.AppendAllText(filePath, Getyyyy_MM_dd_HH_mm_ss() + newLine + content + newLine + newLine + newLine);
        } 
        #endregion


        #region 添加日志 + static void AddLog(string content)
        /// <summary>
        /// 添加日志
        /// </summary>
        /// <param name="content">日志內容</param>
        public static void AddLog(string content)
        {
            string filePath = AppDomain.CurrentDomain.BaseDirectory + Getyyyy_MM_dd() + "_" + SystemFlag + ".log";
            CreateDirectory(filePath);
            File.AppendAllText(filePath, Getyyyy_MM_dd_HH_mm_ss() + " " + content + newLine);
        } 
        #endregion
    }
}
